package com.boc.langxiang.presenter.common;


import android.widget.Toast;

import com.boc.langxiang.App;
import com.boc.langxiang.contract.common.AccountUpdateNickNameContract;
import com.boc.langxiang.entity.request.AccountUpdateNickNameRequest;
import com.boc.langxiang.entity.response.AccountUpdatePasswordContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CreateTime: 2016-06-14上午10:40
 * Author: wjkjinke00@126.com
 * Description:
 */
public class AccountUpdateNickNamePresenter implements AccountUpdateNickNameContract.Presenter {

    private final ApiService mService;
    private AccountUpdateNickNameContract.View mView;

    public AccountUpdateNickNamePresenter(AccountUpdateNickNameContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        mView = view;
    }


    @Override
    public void accountUpdateNickName(final String nickName) {
        mView.showLoading();
        AccountUpdateNickNameRequest request = new AccountUpdateNickNameRequest();
        request.nickname = nickName;
        Observable<BaseResponse<AccountUpdatePasswordContent>> register = mService.accountUpdateNickName(request.params());
        register.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<AccountUpdatePasswordContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<AccountUpdatePasswordContent> response) {
                        Toast.makeText(App.getInstance(), "昵称修改成功", Toast.LENGTH_SHORT).show();
                        UserInfoManager.getInstance().getUserInfo().setNickname(nickName);
                        UserInfoManager.getInstance().setToken(response.getContent().getToken());
                        UserInfoManager.getInstance().setUserInfo(UserInfoManager.getInstance().getUserInfo());
                        mView.endActivity();
                    }
                });
    }
}
